﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using DevExpress.Web.ASPxGridView;
using System.Data;


public partial class Web_Stock : System.Web.UI.Page
{
    List<string> proid = new List<string>();

    protected void Page_Load(object sender, EventArgs e)
    {
        Session["current"] = "Stock";//current page

        if (Session["user"] != null)//check user
        {
            string user = Session["user"].ToString();
            if (user == "viewer")
            {
                Response.Redirect("index.aspx");
            }

        }
        else
        {
            Response.Redirect("index.aspx");
        }

    }

    #region insert
    protected void ASPxGridView1_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
        try
        {
            string user = Session["username"].ToString();
            Bestlife.ERP.Entities.E_Stock stock = new Bestlife.ERP.Entities.E_Stock();

            stock.Proid1 = Convert.ToString(e.NewValues["Pro_Name"]);
            stock.Qtyinstock = Convert.ToDecimal(e.NewValues["P_Qty"]);
            stock.Reorderlevel = Convert.ToString(e.NewValues["Re_Order_Level"]);
            stock.Reorderqty = Convert.ToString(e.NewValues["Re_Order_qty"]);


            Bestlife.ERP.BusinessRules.B_Stock.insert(stock,user);
        }

        catch(Exception)
        {
            //Response.Redirect("404.ASPX");
        }

    }
    #endregion

    #region validation
    protected void ASPxGridView1_RowValidating1(object sender, DevExpress.Web.Data.ASPxDataValidationEventArgs e)
    {
        try
        {
            // Displays the error row if there is at least one error.
            if (e.Errors.Count > 0) e.RowError = "Please, fill all fields.";


            if (e.NewValues["P_Qty"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["P_Qty"],
                "Please Enter Quantity ");
            }
            if (e.NewValues["Re_Order_Level"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Re_Order_Level"],
                "Please Enter Re Order Level Quantity");
            }
            if (e.NewValues["Re_Order_qty"] == null)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Re_Order_qty"],
                "Please Enter Quantity to order");
            }

            if (e.NewValues["Pro_Name"] == null || e.NewValues["Pro_Name"].ToString().Length < 2)
            {
                AddError(e.Errors, ASPxGridView1.Columns["Pro_Name"],
                "Product Name must be at least two characters long.");
            }

        }


        catch
        {
            e.RowError = "Please Check The Data You Entered are in Correct Format";
        }
    }

    void AddError(Dictionary<GridViewColumn, string> errors, GridViewColumn column, string errorText)
    {
        try
        {
            if (errors.ContainsKey(column)) return;
            errors[column] = errorText;
        }
        catch(Exception)
        {
            //Response.Redirect("404.ASPX");
        }
    }

    protected void ASPxGridView1_HtmlRowPrepared1(object sender, ASPxGridViewTableRowEventArgs e)
    {
        try
        {
            if (e.GetValue("P_Qty") != null && e.GetValue("Re_Order_Level") != null)
            {
                decimal QIH = (decimal)e.GetValue("P_Qty");
                decimal Reorder = decimal.Parse(e.GetValue("Re_Order_Level").ToString());
                if (QIH <= Reorder)
                {
                    bool hasError = true;
                    if (hasError)
                    {
                        e.Row.ForeColor = System.Drawing.Color.Red;
                    }
                }
            }
        }
        catch(Exception)
        {
            //Response.Redirect("404.ASPX");
        }

    }
    #endregion
}
